<?php
namespace Plu\Library\Models\Platform\Tag;

use Phalcon\Mvc\Model,
    Phalcon\Db\Column;

class Tags extends Model {
    public $Id;

    public $Name;

    public $PinYin;

    public $AppliedCount;

    public function initialize() {
        $this->setReadConnectionService('db.platform.slave');
    }

    public function getSource() {
        return 'Tags';
    }

    static public function getTagsId($tags) {
        //tags
        $sql = array();
        if (!empty($tags)) {
            $subsql = array();
            $len = count($tags);

            for ($c = 0; $c < $len; $c++) {
                $p = "t{$c}";
                $subsql[] = " Name = :{$p}: ";
                $bind[$p] = $tags[$c];
                $bindTypes[$p] = Column::BIND_PARAM_STR;
            }
            $sql = '(' . join( ' OR ', $subsql) . ')';

            $items = self::find(array(
                $sql,
                'bind'		=> $bind,
                'bindTypes' => $bindTypes,
                'offset'	=> 0,
                'limit'     => 10
            ));

            if (count($items) == 0) {
                return [];
            }

            return array_column($items->toArray(), 'Id');
        }

        return [];
    }
}
?>
